#!/usr/bin/env python

"""
    Npc.py
    This file contains PC information
"""

# Imports
from Creature import *
from GameUI import *
from GameMap import *
from Los import *
from Fov import *

class Player(Object):
    def __init__(self, gameUI, gameMap, gameObjects, posy, posx, char, ticker, color, bolded = False, speed = 1, health = 1):
        self.posy = posy
        self.posx = posx
        self.char = char
        self.ticker = ticker
        self.color = color
        self.bolded = bolded
        self.speed = speed
        self.health = health
        self.trees_chopped = 0

        self.do = {
            'left': (0, -1),
            'down': (1, 0),
            'up': (-1, 0),
            'right': (0, 1)
        }

        self.move_keys = {
            ord('h'): self.do['left'],
            ord('j'): self.do['down'],
            ord('k'): self.do['up'],
            ord('l'): self.do['right'],
            ord('H'): self.do['left'],
            ord('J'): self.do['down'],
            ord('K'): self.do['up'],
            ord('L'): self.do['right'],
            260: self.do['left'],
            258: self.do['down'],
            259: self.do['up'],
            261: self.do['right']
        }

        self.gameUI = gameUI
        self.gameMap = gameMap
        self.gameObjects = gameObjects
        self.los = Bresenham(self.gameUI, self.gameMap, self.gameObjects)
        self.fov = rayCast(self.gameUI, self.gameMap, self.gameObjects, self, 5)

        self.ticker.scheduleTurn(self.speed, self)

        self.running = True
        self.draw()

    def doTurn(self):
        processed = False

        while processed == False and self.health > 0:
            self.gameUI.getKey(True)
            if self.gameUI.key == ord('q') or self.gameUI.key == ord('Q') :
                self.running = False
                processed = True
            elif self.gameUI.key in self.move_keys:
                dy, dx = self.move_keys[self.gameUI.key]
                if self.posy+dy < self.gameMap.height and self.posy+dy >= 0:
                    if self.posx+dx < self.gameMap.width and self.posx+dx >= 0:
                        if self.gameMap.map[self.posy+dy][self.posx+dx].walkable == True:
                            for obj in self.gameObjects:
                                if self.posy+dy == obj.posy and self.posx+dx == obj.posx and self.__class__.__name__ != obj.__class__.__name__:
                                    obj.health -= 1
                                    if obj.health == 0:
                                        obj.clear()
                                        self.gameObjects.remove(obj)
                                    processed = True
                            if processed == False:
                                self.clear()
                                self.move(dy, dx)
                                self.speed = 1
                                processed = True
                        elif self.gameMap.map[self.posy+dy][self.posx+dx].char == 'T':
                            self.gameMap.map[self.posy+dy][self.posx+dx].char = '='
                            self.gameMap.map[self.posy+dy][self.posx+dx].color = 6
                            self.gameMap.map[self.posy+dy][self.posx+dx].walkable = True
                            self.gameMap.map[self.posy+dy][self.posx+dx].block_los = False
                            self.gameMap.drawMap()
                            self.speed = 10
                            self.trees_chopped += 1
                            processed = True
                self.fov.computeFov()
            elif self.gameUI.key == ord('o') or self.gameUI.key == ord('O'):
                self.gameUI.clearLine(self.gameMap.height+2)
                self.gameUI.putString(self.gameMap.height+2, 0, "Open door - which direction?", 0)
                self.gameUI.getKey(True)
                if self.gameUI.key in self.move_keys:
                    dy, dx = self.move_keys[self.gameUI.key]
                    if self.gameMap.map[self.posy+dy][self.posx+dx].char == '+':
                        self.gameMap.map[self.posy+dy][self.posx+dx].char = '/'
                        self.gameMap.map[self.posy+dy][self.posx+dx].color = 6
                        self.gameMap.map[self.posy+dy][self.posx+dx].walkable = True
                        self.gameMap.map[self.posy+dy][self.posx+dx].block_los = False
                        self.gameMap.drawMap()
                        processed = True
                    else:
                        self.gameUI.clearLine(self.gameMap.height+2)
                        self.gameUI.putString(self.gameMap.height+2, 0, "No closed doors at this direction!", 4, True)
            elif self.gameUI.key == ord('c') or self.gameUI.key == ord('C'):
                self.gameUI.clearLine(self.gameMap.height+2)
                self.gameUI.putString(self.gameMap.height+2, 0, "Close door - which direction?", 0)
                self.gameUI.getKey(True)
                if self.gameUI.key in self.move_keys:
                    dy, dx = self.move_keys[self.gameUI.key]
                    if self.gameMap.map[self.posy+dy][self.posx+dx].char == '/':
                        self.gameMap.map[self.posy+dy][self.posx+dx].char = '+'
                        self.gameMap.map[self.posy+dy][self.posx+dx].color = 6
                        self.gameMap.map[self.posy+dy][self.posx+dx].walkable = False
                        self.gameMap.map[self.posy+dy][self.posx+dx].block_los = True
                        self.gameMap.drawMap()
                        processed = True
                    else:
                        self.gameUI.clearLine(self.gameMap.height+2)
                        self.gameUI.putString(self.gameMap.height+2, 0, "No opened doors at this direction!", 4, True)
            elif self.gameUI.key == ord('t') or self.gameUI.key == ord('T'):
                x = self.posx
                y = self.posy

                self.gameUI.key = ''

                while self.gameUI.key != ord('t') or self.gameUI.key == ord('T'):
                    self.los.check(self.posx, x, self.posy, y)
                    if self.los.blocked == False:
                        self.gameUI.putChar(y, x, '*', 2, True)
                    else:
                        self.gameUI.putChar(y, x, '*', 4, True)

                    self.gameUI.getKey(True)
                    if self.gameUI.key in self.move_keys:
                        self.gameUI.putChar(y, x, self.gameMap.map[y][x].char, self.gameMap.map[y][x].color, False)
                        for obj in self.gameObjects:
                            if y == obj.posy and x == obj.posx:
                                obj.draw()
                        self.draw()
                        dy, dx = self.move_keys[self.gameUI.key]
                        if x+dx < self.gameMap.width and x+dx >= 0 and self.gameMap.map[y+dy][x+dx].fov == True:
                            x += dx
                        if y+dy < self.gameMap.height and y+dy >= 0 and self.gameMap.map[y+dy][x+dx].fov == True:
                            y += dy
                self.gameUI.putChar(y, x, self.gameMap.map[y][x].char, self.gameMap.map[y][x].color, False)
                self.draw()

        self.draw()
        self.gameUI.clearLine(self.gameMap.height+2)
        self.ticker.scheduleTurn(self.speed, self)
        self.speed = 1